package cn.easy.kitchen.service;

import cn.easy.base.bean.PageBean;
import cn.easy.base.service.BaseService;
import cn.easy.kitchen.domain.StatMaterialPrice;
import cn.easy.kitchen.repository.StatMaterialPriceDao;
import cn.easy.kitchen.utils.ProjectUtil;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class StatMaterialPriceService extends BaseService<StatMaterialPrice> {
    @Autowired
    private StatMaterialPriceDao	statMaterialPriceDao;

    public Page<StatMaterialPrice> search(Map<String, Object> searchParams, PageBean pageBean) {
        return statMaterialPriceDao.findAll(spec(searchParams), pageBean.toPageRequest(new Sort(Direction.DESC, "createdAt")));
    }

    public List<StatMaterialPrice> search(Map<String, Object> searchParams, Sort... sort) {
        return statMaterialPriceDao.findAll(spec(searchParams), sort == null || sort.length == 0  ? new Sort(Direction.DESC, "createdAt") : sort[0]);
    }

    public List<StatMaterialPrice> searchByParams(Map<String, Object> searchParams, String type) {
        StringBuilder sql = new StringBuilder("select ");
        if ("thisYear".equals(type) || "default".equals(type)) {
            sql.append("DATE_FORMAT(data_on,'%Y-%m'), avg(pork),avg(corn),avg(romaine_lettuce),avg(rice) ");
            sql.append("from ck_stat_material_price ");
            sql.append("where data_on >=:start_dataOn ");
            sql.append("and data_on <=:end_dataOn ");
            sql.append("GROUP BY DATE_FORMAT(data_on,'%Y-%m') ");
            sql.append("order by DATE_FORMAT(data_on,'%Y-%m') asc LIMIT 5 ");
        } else {
            sql.append("DATE_FORMAT(data_on,'%m-%d'), pork, corn, romaine_lettuce, rice ");
            sql.append("from ck_stat_material_price ");
            sql.append("where data_on >=:start_dataOn ");
            sql.append("and data_on <=:end_dataOn ");
            sql.append("order by data_on asc LIMIT 5 ");
        }
        Query query = entityManager.createNativeQuery(sql.toString());
        query.setParameter("start_dataOn", ProjectUtil.formatDate(searchParams.get("GTE_dataOn")));
        query.setParameter("end_dataOn", ProjectUtil.formatDate(searchParams.get("LTE_dataOn")));

        List<Object[]> list = query.getResultList();
        List<StatMaterialPrice> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Object[] ob = list.get(i);
            StatMaterialPrice statMaterialPrice = new StatMaterialPrice();
            statMaterialPrice.setDataOnStr(ob[0].toString());
            statMaterialPrice.setPork(ProjectUtil.getBigDecimalVal(ob[1]).setScale(2, BigDecimal.ROUND_HALF_UP));
            statMaterialPrice.setCorn(ProjectUtil.getBigDecimalVal(ob[2]).setScale(2, BigDecimal.ROUND_HALF_UP));
            statMaterialPrice.setRomaineLettuce(ProjectUtil.getBigDecimalVal(ob[3]).setScale(2, BigDecimal.ROUND_HALF_UP));
            statMaterialPrice.setRice(ProjectUtil.getBigDecimalVal(ob[4]).setScale(2, BigDecimal.ROUND_HALF_UP));
            resultList.add(statMaterialPrice);
        }
        return resultList;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(String id) {
        statMaterialPriceDao.delete(id);
    }

    public StatMaterialPrice getOne(String id) {
        if (StringUtils.isBlank(id)) {
            return null;
        }
        return statMaterialPriceDao.findOne(id);
    }

    public List<StatMaterialPrice> findAll(List<String> idList) {
        return Lists.newArrayList(statMaterialPriceDao.findAll(idList));
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public StatMaterialPrice save(StatMaterialPrice statMaterialPrice) {
        return statMaterialPriceDao.save(statMaterialPrice);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void save(List<StatMaterialPrice> statMaterialPriceList) {
        statMaterialPriceDao.save(statMaterialPriceList);
    }
}


